package com.ahuzdw.com.linklist;

import java.time.temporal.Temporal;

/**
 * Author: Zhang Dongwei
 * Date: 2021/12/27 12:40
 */
public class SingleLinkedList {
    public static void main(String[] args) {
        SingleLinkedListDemo singleLinkedListDemo = new SingleLinkedListDemo();
//        System.out.println(singleLinkedListDemo.getHead().name);

        // 创建节点
        HeroNode node1 = new HeroNode(1, "刘备");
        HeroNode node2 = new HeroNode(2, "张飞");
        HeroNode node3 = new HeroNode(3, "关羽");
        HeroNode node4 = new HeroNode(4, "吕布");
        HeroNode node5 = new HeroNode(5, "貂蝉");
        // 添加节点
//
//        singleLinkedListDemo.addReorder(node5);
//        singleLinkedListDemo.addReorder(node1);
//        singleLinkedListDemo.addReorder(node3);
//        singleLinkedListDemo.addReorder(node2);
//        singleLinkedListDemo.addReorder(node4);

//        singleLinkedListDemo.addHeroNode(node1);
//        singleLinkedListDemo.addHeroNode(node2);
//        singleLinkedListDemo.addHeroNode(node3);
//        singleLinkedListDemo.addHeroNode(node4);
//        singleLinkedListDemo.addHeroNode(node5);

        singleLinkedListDemo.addHeroNode1(node1);
        singleLinkedListDemo.addHeroNode1(node2);
        singleLinkedListDemo.addHeroNode1(node3);
        singleLinkedListDemo.addHeroNode1(node4);
        singleLinkedListDemo.addHeroNode1(node5);

        // 显示节点
        singleLinkedListDemo.showLinkedList1(singleLinkedListDemo.getHead());

        singleLinkedListDemo.updateLinkedList(new HeroNode(6, "haha"));
        System.out.println("================================");
        singleLinkedListDemo.showLinkedList1(singleLinkedListDemo.getHead());

//        System.out.println(singleLinkedListDemo);
//        System.out.println(singleLinkedListDemo.getClass().toString());
//        System.out.println(singleLinkedListDemo.getHead().next.id);
//        singleLinkedListDemo.addHeroNode1(singleLinkedListDemo, node1);

        // 删除5
//        System.out.println("------------------------");
//        singleLinkedListDemo.delLinkedList(5);
//        singleLinkedListDemo.showLinkedList(singleLinkedListDemo.getHead());

    }
}

class SingleLinkedListDemo{
    private HeroNode head = new HeroNode(0,"head");

    // 头节点是用来方便查找
    // 有个方法来获取头结点
    public HeroNode getHead(){
        return head;
    }

    // 添加结点
    public void addHeroNode(HeroNode heroNode){
        // 因为head结点是不能动的，所以需要一个辅助结点temp
        HeroNode temp = head;
        // 添加数据是从链表的最后添加，如何确认最后的位置，只能.next这样遍历下去，直到值为null就找到位置
        while (true){
            if(temp.next == null){
                break;
            }
            temp = temp.next;
        }
        temp.next = heroNode;
    }

    // 添加节点1
    public void addHeroNode1(HeroNode heroNode){
        HeroNode temp = head;
        while (temp.next != null){
            temp = temp.next;
        }
        temp.next = heroNode;
    }

    // 显示链表
    public void showLinkedList(HeroNode head){
        // 判断链表是否为空
        if(head.next == null){
            System.out.println("链表为空");
        }
        // 因为我们的头结点是不能动的，所以需要一个辅助变量来遍历
        HeroNode temp = head.next;
        while (true){
            if(temp == null)
                break;
            System.out.println(temp);
            temp = temp.next;
        }
    }

    // 显示链表1
    public void showLinkedList1(HeroNode head){
        if(head.next == null){
            System.out.println("链表为空");
        }
        HeroNode temp = head;
        while (temp.next != null){
//            System.out.println(temp.next.name);
            System.out.println(temp.next);
            temp = temp.next;
        }
    }

    // 1刘备-》2张飞-》关羽
    // if(temp.id = heroNode.id)   temp.name = heroNode.name
    // 修改链表节点数据
    public void updateLinkedList(HeroNode heroNode){
        // 判断链表是否为空
        if(head.next == null){
            System.out.println("链表为空");
            return;
        }
        // 定义一个辅助结点
        HeroNode temp = head.next;
        boolean flag = false;
        // 是否找到对应结点
        while (true){
            // 防止空指针异常
            if(temp == null){
                break;
            }
            // 如果找到
            if(temp.id == heroNode.id){
                flag = true;
                break;
            }
        }
        if(flag){
            temp.name = heroNode.name;
        }else {
            System.out.println("没有找到id为"+heroNode.id);
        }
        temp = temp.next;  // temp后移
    }


    // 删除结点
    public void delLinkedList(int id){
        HeroNode temp = head;
        boolean flag = false;
        // 找到待删除结点的前一个结点
        while (true){
            // 已经遍历到最后，需要退出循环
            if(temp.next == null){
                break;
            }
            // 找到
            if(temp.next.id == id){
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if(flag){
            // 为什么是temp.next.next
            temp.next = temp.next.next;
        }else {
            System.out.println("没有找到id为"+id);
        }
    }

    // 排序加入
    public void addReorder(HeroNode heroNode){
        HeroNode temp = head;
        boolean flag = false;
        while (true){
            //链表到最后
            if(temp.next == null){
                flag = true;
                break;
            }else if(temp.next.id > heroNode.id){
                flag = true;
                break;
            }else if(temp.next.id == heroNode.id){
                break;
            }
            temp = temp.next;
        }
        if(flag){
            // 新的节点指向temp指向下一个节点
            heroNode.next = temp.next;
            // temp节点指向新的节点
            temp.next = heroNode;
        }else {
            // ID重复
            System.out.println("id"+heroNode.id+"重复");
        }
    }
}
class HeroNode{
    public int id;
    public String name;
    public HeroNode next;//用来指向下一个节点

    public HeroNode(int id, String name) {
        super();
        this.id = id;
        this.name = name;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}